package com.jingdianjichi.oss.config;

import com.jingdianjichi.oss.adapter.StorageAdapter;
import com.jingdianjichi.oss.adapter.AliStorageAdapter;
import com.jingdianjichi.oss.adapter.MinioStorageAdapter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;


/**
 * 文件存储config
 *
 */
@Configuration
@RefreshScope
public class StorageConfig {

    @Value("${storage.service.type}")
    private String storageType;

    //适配器模式，也可以使用工厂模式或者spring自动注入（注意接口实现类区分名称）


//    public static void main(String[] args) {
//        Set<String> concurrentSet = ConcurrentHashMap.newKeySet();
//
//        Set<String> HashSet = new HashSet<>();
//
//        // 多线程环境下的操作示例
//        Runnable task = () -> {
//            for (int i = 0; i < 1000; i++) {
////                concurrentSet.add(Thread.currentThread().getName() + "-" + i);
//                HashSet.add(Thread.currentThread().getName() + "-" + i);
//            }
//        };
//
//        Thread thread1 = new Thread(task, "Thread1");
//        Thread thread2 = new Thread(task, "Thread2");
//
//        thread1.start();
//        thread2.start();
//
//        try {
//            thread1.join();
//            thread2.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
////        System.out.println("Set size: " + concurrentSet.size());
//        System.out.println("HashSet size: " + HashSet.size());
//    }

//    public static void main(String[] args) {
//        Thread t1 = new Thread(() -> {
//            while (true) {
//                if (Thread.currentThread().isInterrupted()) {
//                    System.out.println("Thread is interrupted");
//                    break;
//                }
//                System.out.println("Thread is running");
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    Thread.currentThread().interrupt();  // 重新设置中断状态
//                }
//            }
//        });
//        t1.start();
//
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        t1.interrupt();  // 中断 t1 线程
//    }

    @Bean
    @RefreshScope
    public StorageAdapter storageService() {
        if ("minio".equals(storageType)) {
            return new MinioStorageAdapter();
        } else if ("aliyun".equals(storageType)) {
            return new AliStorageAdapter();
        } else {
            throw new IllegalArgumentException("未找到对应的文件存储处理器");
        }
    }

}
